﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MediaPortal.Configuration;
using System.IO;


namespace Logging
{
    public enum MessageType
    {
        Info,
        Debug,
        Error,
        Warning
    }

    public static class Logger
    {
        private static string _LogFile;
        private static StreamWriter _writer;

        static Logger()
        {
            _LogFile=MediaPortal.Configuration.Config.GetDirectoryInfo(Config.Dir.Log).FullName+"\\MusicEvolution.log";
            if (File.Exists(_LogFile))
            {
                File.Copy(_LogFile,MediaPortal.Configuration.Config.GetDirectoryInfo(Config.Dir.Log).FullName+"\\MusicEvolution.bak.log");
                File.Delete(_LogFile);
            }
            FileStream fs=File.Create(_LogFile);
            _writer = new StreamWriter(fs);
        }

        public static void LogMessage(MessageType Type,string Sending, string message)
        {
            string date = DateTime.Now.ToShortDateString() + DateTime.Now.ToShortTimeString();
            string prefix = "";
            switch (Type)
            {
                case (MessageType.Info):
                    {
                        prefix = "[INFO  ]";
                        break;
                    }
                case MessageType.Error:
                    {
                        prefix = "[ERROR]";
                        break;
                    }
                case MessageType.Debug:
                    {
                        prefix = "[DEBUG]";
                        break;
                    }
                case MessageType.Warning:
                    {
                        prefix = "[WARNI]";
                        break;
                    }
            }
            if (Type!=MessageType.Debug)
            {
                _writer.WriteLine(date+"\t"+prefix+"["+Sending+"\t]"+message);
            }
            else
            {
                //TODO: find debug flag in mp configuration
                _writer.WriteLine(date+"\t"+prefix+"["+Sending+"\t]"+message);
            }
        }

    }
}
